<?php

namespace drivers;

class config implements \ArrayAccess {

    private $configData;

    private $dir;

    public function getConfig($key){
        return $this->offsetGet($key);
    }

    public function __construct($default = [])
    {
        $this->dir = isset($default['dir'])&&!empty($default['dir']) ? $default['dir'] : 'config';
    }

    public function offsetExists($offset)
    {
        // TODO: Implement offsetExists() method.
        return isset($this->configData[$offset]);
    }

    public function offsetGet($offset)
    {
        // TODO: Implement offsetGet() method.
        if(isset($this->configData[$offset])){
            return $this->configData[$offset];
        } else if (!isset($this->configData[$offset])){
            $file = __ROOT__.$this->dir.'/'.$offset.'.php';
            if(file_exists($file)){
                $config = include($file);
                $this->configData[$offset] = $config;
                return $config;
            }else{
                throw new TException("配置文件".$offset."不存在，请仔细检查");
            }
        }
    }

    public function offsetSet($offset, $value)
    {
        // TODO: Implement offsetSet() method.
        return false;


    }

    public function offsetUnset($offset)
    {
        // TODO: Implement offsetUnset() method.
        if (isset($this->configData[$offset])) unset($this->configData[$offset]);
    }

}